<template lang="html">
  <ul class="list"
      @touchstart = "handleTouchStart"
      @touchmove = "handleTouchMove"
      @touchend = "handleTouchEnd"
      @click="handleLetClick">
    <li class="item" @click="handleLetClick" ref="▲">▲</li>
    <li class="item"
    v-for="item of letters"
    :key="item"

    :ref = "item"
    >{{item}}</li>
  </ul>
</template>

<script>
export default {
  name: 'cityAlphabet',
  props: {
    cities: Object
  },
  computed: {
    letters () {
      const letters = []
      //for in 遍历的是数组、对象的索引即键名
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  updated () {
    this.startY = this.$refs['▲'].offsetTop
    console.log(this.startY)
  },
  methods: {
    handleLetClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchStart () {
      this.touchStatus = true
    },
    handleTouchMove (e) {
      if (this.touchStatus) {
        // 节流函数,提高性能
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 80
          const index = Math.floor((touchY - this.startY) / 20)
          console.log(touchY + 't')
          console.log(this.startY + 's')
          if (index > 0 && index <= this.letters.length) {
            this.$emit('change', this.letters[index - 1])
          } else if (index === 0) {
            this.$emit('change', '▲')
          }
        }, 16)
      }
    },
    handleTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl';
  .list
    display: flex
    flex-direction: column
    justify-content: center
    position: absolute
    top: 1.58rem;
    right: .0rem
    bottom: 0
    width: .9rem
    .item
      text-align: center
      line-height: .4rem
      color: $bgColor
</style>
